<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla modelform library
jimport('joomla.application.component.modelform');
 
/**
 * HelloWorld Model
 */
class ForumModelPost extends JModelForm
{
	/**
	 * Model context string.
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $_context = 'com_forum.post';
	protected $data;
	protected $_topic;
	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @since	1.6
	 */
	protected function populateState()
	{
		// Load the object state.
		$id	= JRequest::getInt('id');
		$this->setState('post.id', $id);
		$tid = JRequest::getInt('tid');
		$this->setState('tid', $tid);
	}
	
	public function save($data)
	{
		$table = $this->getTable();
		if (!$table->bind($data)) {
			$this->setError(JText::sprintf('BIND FAILED', $user->getError()));
			return false;
		}
		if (!$table->store()) {
			$this->setError($table->getError());
			return false;
		}
		return $table->id;
	}
	
	/**
	 * Returns a reference to the a Table object, always creating it.
	 *
	 * @param	type	The table type to instantiate
	 * @param	string	A prefix for the table class name. Optional.
	 * @param	array	Configuration array for model. Optional.
	 * @return	JTable	A database object
	 * @since	1.6
	 */
	public function getTable($type = 'Post', $prefix = 'ForumTable', $config = array()) 
	{
		return JTable::getInstance($type, $prefix, $config);
	}

	/**
	 * Method to get the profile form.
	 *
	 * The base form is loaded from XML and then an event is fired
	 * for users plugins to extend the form with extra fields.
	 *
	 * @param	array	$data		An optional array of data for the form to interogate.
	 * @param	boolean	$loadData	True if the form is to load its own data (default case), false if not.
	 * @return	JForm	A JForm object on success, false on failure
	 * @since	1.6
	 */
	public function getForm($data = array(), $loadData = true)
	{
		// Get the form.
		$form = $this->loadForm('com_form.post', 'post', array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form)) {
			return false;
		}

		return $form;
	}


	/**
	 * Method to get the data that should be injected in the form.
	 *
	 * @return	mixed	The data for the form.
	 * @since	1.6
	 */
	protected function loadFormData()
	{
		//$data = (array)JFactory::getApplication()->getUserState('com_forum.post.data', array());
		// Check the session for previously entered form data.
		$data = JFactory::getApplication()->getUserState('com_forum.post.data', array());
		if (empty($data)) 
		{
			$data = $this->getItem();
		}
		return $data;
	}

	function getItem()
	{
		$tid = $this->getState('tid');
		$data = $this->getTable();
		$data->tid = $tid;
		$user			= JFactory::getUser();
		$loginUserId	= (int) $user->get('id');
		if($user->id != 0) $data->created_by_alias = $user->get('name');
		return $data;
	}


	/**
	 * Method to get a topic.
	 *
	 * @param	integer	The id of the object to get.
	 *
	 * @return	mixed	Object on success, false on failure.
	 */
	public function getTopic()
	{
		if ($this->_topic === null)
		{
			$this->_topic = false;

			$id = $this->getState('topic.id');

			// Get a level row instance.
			$table = JTable::getInstance('Topic', 'ForumTable');

			// Attempt to load the row.
			if ($table->load($id))
			{
				// Check published state.
				if ($published = $this->getState('filter.published'))
				{
					if ($table->state != $published) {
						return $this->_topic;
					}
				}

				// Convert the JTable to a clean JObject.
				$properties = $table->getProperties(1);
				$this->_topic = JArrayHelper::toObject($properties, 'JObject');
			}
			elseif ($error = $table->getError()) {
				$this->setError($error);
			}
		}
		return $this->_topic;
	}			
	
}
